package io.renren.modules.hos.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.ShopCarEntity;
import io.renren.modules.hos.service.OrderMedicineService;
import io.renren.modules.hos.service.ShopCarService;
import io.renren.modules.hos.vo.OrderVo;
import io.renren.modules.hos.vo.ShopCarVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.hos.entity.OrderEntity;
import io.renren.modules.hos.service.OrderService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author ryf
 * @email 2559228480@qq.com
 * @date 2021-12-22 21:50:18
 */
@RestController
@RequestMapping("hos/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private OrderMedicineService orderMedicineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("hos:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("hos:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("hos:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("hos:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 购买的药品记录
     */
    @RequestMapping("/orderList")
    public R orderMedicineList(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryOrder(ShiroUtils.getHosUserId(), params);

        return R.ok().put("page", page);
    }

    /**
     * 购物车药品形成订单
     */
    @RequestMapping("/createOrder")
    public R createOrder(){
        List<ShopCarEntity> shopCarEntities = shopCarService.queryById(ShiroUtils.getHosUserId());

        //生成订单
        String orderNum = orderService.createOrder();
        OrderEntity orderEntity = orderService.selectByOrderNum(orderNum);

        //添加到订单商品表
        orderMedicineService.saveShopCar(shopCarEntities, orderEntity.getId());

        OrderVo orderVo = orderService.getOrderVo(orderEntity);

        return R.ok().put("data", orderVo);
    }

    @RequestMapping("/cancelOrder")
    public R cancelOrder(@RequestParam Long orderId){
        OrderEntity orderEntity = orderService.getById(orderId);
        if (orderEntity.getPayStatus() == 0){
            orderService.updateById(orderEntity);
            return R.ok().put("data", "取消成功");
        }else {
            return R.error().put("data", "取消失败");
        }

    }
}
